package com.hl.web;

import com.hl.service.ZixunlunboService;
import com.hl.util.GeneralResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/zixunlunbo")
@CrossOrigin
public class Zixunlunbo {
    String path = null;
    File toFile = null;
    @Autowired
    private ZixunlunboService zixunlunboService;
    @RequestMapping("/zixunlunbos")
    public GeneralResult zixunlunbos(){
        return GeneralResult.success(zixunlunboService.carouselimages());
    }

    @RequestMapping("/selectlunbo")
    public GeneralResult selectlunbo(){
        return GeneralResult.success(zixunlunboService.selectlunbo());
    }

    @RequestMapping("/upload")
    public GeneralResult upload(MultipartFile file) throws IOException {
        toFile = new File("C:\\Users\\Administrator\\Desktop\\sfzy-vue\\vue\\sfzy\\public\\images\\"+file.getOriginalFilename());
        file.transferTo(toFile);
        path="/images/"+file.getOriginalFilename();
        System.out.println(file.getOriginalFilename());
        System.out.println(toFile.getAbsolutePath());
        return GeneralResult.success();
    }

    @RequestMapping("/insertlunbo")
    public GeneralResult insertlunbo(@RequestBody Map<String,String> map){
        String image_url = map.get("image_url");
        if (image_url==null|| image_url.isEmpty()){
            return GeneralResult.fail();
        }
        return GeneralResult.success("添加成功",zixunlunboService.insertlunbophotos(image_url));
    }

    @RequestMapping("/deletelunbo")
    public GeneralResult deletelunbo(@RequestBody Map<String,String> map){
        Integer id = Integer.parseInt(map.get("id"));
        return GeneralResult.success("删除成功",zixunlunboService.deletelunbophotos(id));
    }
}
